package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseShop;
import com.ruoyi.system.service.IBaseShopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店Controller
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/system/shop")
public class BaseShopController extends BaseController
{
    @Autowired
    private IBaseShopService baseShopService;

    /**
     * 查询门店列表
     */
    @PreAuthorize("@ss.hasPermi('system:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseShop baseShop)
    {
        startPage();
        List<BaseShop> list = baseShopService.selectBaseShopList(baseShop);
        return getDataTable(list);
    }

    /**
     * 导出门店列表
     */
    @PreAuthorize("@ss.hasPermi('system:shop:export')")
    @Log(title = "门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseShop baseShop)
    {
        List<BaseShop> list = baseShopService.selectBaseShopList(baseShop);
        ExcelUtil<BaseShop> util = new ExcelUtil<BaseShop>(BaseShop.class);
        util.exportExcel(response, list, "门店数据");
    }

    /**
     * 获取门店详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shop:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return success(baseShopService.selectBaseShopByShopId(shopId));
    }

    /**
     * 新增门店
     */
    @PreAuthorize("@ss.hasPermi('system:shop:add')")
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseShop baseShop)
    {
        SysUser user = getLoginUser().getUser();
        baseShop.setUserId(user.getUserId());
        baseShop.setDeptId(user.getDeptId());
        baseShop.setCreateBy(user.getUserName());
        return toAjax(baseShopService.insertBaseShop(baseShop));
    }

    /**
     * 修改门店
     */
    @PreAuthorize("@ss.hasPermi('system:shop:edit')")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseShop baseShop)
    {
        return toAjax(baseShopService.updateBaseShop(baseShop));
    }

    /**
     * 删除门店
     */
    @PreAuthorize("@ss.hasPermi('system:shop:remove')")
    @Log(title = "门店", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(baseShopService.deleteBaseShopByShopIds(shopIds));
    }
}
